import Vue from 'vue'
import Vuex from 'vuex'
import config from '@/config'
import vuexStorage from '@/util/vuexStorage'
Vue.use(Vuex)
Vue.prototype.$config = config
export default new Vuex.Store({
  state: () => ({
    loading: true,
    pageLoading: false,
    settled: '',
    merchantId: '',
    systemId: '',
    token: '',
    userId: '',
    userTel: '',
    content: '',
    // submit: '',
    verifyState: '',
    classes: ''
  }),
  getters: {
  },
  mutations: {
    setClasses (state, opt) {
      state.classes = opt
    },
    pageLoading (state, opt) {
      state.pageLoading = opt
    },
    setSettled (state, opt) {
      state.settled = opt
    },
    setVerifyState (state, opt) {
      state.verifyState = opt
    },
    setMerchant (state, opt) {
      state.merchantId = opt
    },
    setLoading (state, opt) {
      state.loading = opt
    },
    setSystemId (state, opt) {
      state.systemId = opt
    },
    setToken (state, opt) {
      state.token = opt
    },
    setUserid (state, opt) {
      state.userId = opt
    },
    setTel (state, opt) {
      state.setTel = opt
    },
    setContent (state, opt) {
      state.content = opt
    }
    // setsubmit (state, opt) {
    //   state.submit = opt
    // }
  },
  actions: {
    getUserInfo ({ commit, state }) {
      return new Promise(resolve => {
        window._tkAjax.get(`${config.baseUrl}/merchant/getMerchantInfoById`).then(({ data }) => {
          if (data.content) {
            commit('setSettled', data.content.merchantState)
            commit('setMerchant', data.content.merchantId)
          } else {
            commit('setSettled', '')
            commit('setMerchant', '')
          }
          resolve()
        }).catch(() => {
          commit('setSettled', '')
          commit('setMerchant', '')
          commit('setToken', '')
          commit('setUserid', '')
          resolve()
        })
      })
    }
  },
  plugins: [vuexStorage.init(config.storageKeys, config.sessionKeys, config.cookieKeys)]
})
